package com.xiaoqiang.proxy.jdk;

import com.xiaoqiang.proxy.Hello;
import com.xiaoqiang.proxy.HelloDefault;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 小强
 * @since 2019/1/3 22:38
 */
public class JDKProxyHandler implements InvocationHandler {

    private Object target;

    public JDKProxyHandler(Object o) {
        target = o;
    }
    public static void main(String[] args) {
        Hello hello = new HelloDefault();
        hello = (Hello) Proxy.newProxyInstance(hello.getClass().getClassLoader(), hello.getClass().getInterfaces(), new JDKProxyHandler(hello));
        hello.say("张三");
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before invoke");
        Object result = method.invoke(target, args);
        System.out.println("after invoke");
        return result;
    }
}
